import {
  ListDeleteReq,
  PaginationProps
} from '@/common/types';
import { AxiosProgressEvent } from 'axios';
import apiClient from '../apiClient';

export enum TestApi {
  CreateTask = '/openapi/v2/app/test/{appId}/task/create',
  GetListTask = '/openapi/v2/app/test/{appId}/task/list',
  FilterTask = '/openapi/v2/app/test/{appId}/task/filter',
  DeleteTask = '/openapi/v2/app/test/task/delete',
  IssuedTask = '/openapi/v2/app/test/task/{taskId}/issued',
  GetTestFile = '/openapi/v2/app/test/file/{type}',
  DownloadTest = '/openapi/v2/app/test/result/download',
  ImportResult = '/openapi/v2/app/test{taskId}/result/import',

}


const CreateTask = (
  appId: string,
  taskName: string,
  data: FormData
) => {
  console.log(data, '--formData')
  let url = `${TestApi.CreateTask.replace('{appId}', appId)}`;
  url += `?taskName=${taskName}`;
  return apiClient.post({
    url: url,
    withCredentials: true,
    timeout: 600000,
    data,
    headers: { 'Content-Type': 'multipart/form-data' },
  });
}


// 获取列表
const GetListTask = (appId: string, pagingInfo: PaginationProps) => {
  let url = `${TestApi.GetListTask.replace('{appId}', appId)}`;

  let params = {
    ...pagingInfo
  }

  return apiClient.post({ url, data: params });
};
// 获取筛选列表
const FilterTask = (appId: string) => {
  let url = `${TestApi.FilterTask.replace('{appId}', appId)}`;
  return apiClient.get({ url });
};

// 重新下发任务
const IssuedTask = (taskId: string) => {
  let url = `${TestApi.IssuedTask.replace('{taskId}', taskId)}`;
  return apiClient.post({ url });
};

// 下载文件
const GetTestFile = (type: string, name?: string) => {
  let url = `${TestApi.GetTestFile.replace('{type}', type)}`;
  if (name) {
    url += `?resultFileName=${name}`;
  }
  return apiClient.get({ url });
};

// 下载批量文件
const DownloadTest = (data: ListDeleteReq) => {
  return apiClient.post({ url: TestApi.DownloadTest, data });
};
// 删除文件
const DeleteTask = (data: ListDeleteReq) => {
  return apiClient.post({ url: TestApi.DeleteTask, data });
};

export default {
  CreateTask,
  GetListTask,
  GetTestFile,
  IssuedTask,
  DeleteTask,
  DownloadTest,
  FilterTask
};